#ifndef _FlashToEEP_H_
#define _FlashToEEP_H_

#include <stdint.h>
#include "rtctime.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define FLASH_EEP_ERR_NONE 0
#define FLASH_EEP_ERR_SECSIZE 1 
#define FLASH_EEP_ERR_EEPSIZE 2 
#define FLASH_EEP_ERR_BLKSIZE 3 
#define FLASH_EEP_ERR_DATSIZE 4 
#define FLASH_EEP_ERR_EEPBASE 5 
#define FLASH_EEP_ERR_DATOVE 6	
#define FLASH_EEP_ERR_DATOVK 7	
#define FLASH_EEP_ERR_BLKOVE 8	
#define FLASH_EEP_ERR_DATPER 9	
#define FLASH_EEP_ERR_EMPTY 10	
#define FLASH_EEP_ERR_DATBL 11	
#define FLASH_EEP_ERR_EEPSST 12 
#define FLASH_EEP_ERR_EXTPAG 13 
#define FLASH_EEP_ERR_READ 14	

	typedef struct
	{
		uint32_t Site;	  
		uint32_t Blocks;  
		uint16_t Err;	  
		uint16_t Checked; 
	} FlashEepInfo_Type;

	typedef struct
	{
		uint32_t SectorSize;						   
		uint32_t EepSize;							   
		uint32_t DataBlockSize;						   
		uint32_t DataSize;							   
		uint32_t EepBase;							   
		uint16_t *Data;								   
		FlashEepInfo_Type *Info;					   
		void (*Erase)(uint32_t AddrBase);			   
		void (*Program)(uint32_t Addr, uint32_t Data); 
		void (*Unlock)(void);						   
		void (*Lock)(void);							   
	} FlashEepCfg_Type;

	uint16_t FlashEepRead(const FlashEepCfg_Type *EepCfg);
	uint16_t FlashEepSave(const FlashEepCfg_Type *EepCfg);

	uint16_t FlashMulEepSave(const FlashEepCfg_Type *EepCfg);
	uint16_t FlashMulEepRead(const FlashEepCfg_Type *EepCfg);

	typedef struct
	{
		uint32_t Site;	  
		uint32_t Blocks;  
		uint16_t Err;	  
		uint16_t Checked; 
	} ExtFlashEepInfo_Type;

	typedef struct
	{
		uint32_t EepBase;												
		uint32_t EepSize;												
		uint32_t SectorSize;											
		uint32_t DataBlockSize;											
		uint32_t DataSize;												
		void *Data;														
		ExtFlashEepInfo_Type *Info;										
		uint8_t (*Erase)(uint32_t AddrBase);							
		uint8_t (*Program)(uint32_t Addr, uint8_t *Data, uint16_t Len); 
		uint8_t (*Read)(uint32_t Addr, uint8_t *Data, uint16_t Len);
	} ExtFlashEepCfg_Type;

	uint16_t ExtFlashMulEepSave(const ExtFlashEepCfg_Type *EepCfg);
	uint16_t ExtFlashMulEepRead(const ExtFlashEepCfg_Type *EepCfg);

	typedef struct
	{
		uint16_t blocks;
		uint16_t site_next;
		uint16_t fsize;
		uint8_t log_sec_num;
		uint8_t tab_sec_num;
		uint8_t tab_sec_now;
		uint8_t tab_sec_sn;
		uint8_t tab_err;
		uint8_t cfg_err;
	} par_log_info_type;

	typedef struct
	{
		uint32_t addr_base;		 
		uint32_t rom_size;		 
		uint16_t sector_size;	 
		uint16_t unit_size;		 
		uint16_t dat_size;		 
		uint16_t par_amount;	 
		void *par_tab;			 
		void *buff;				 
		par_log_info_type *info; 
		uint8_t (*Erase)(uint32_t AddrBase);
		uint8_t (*Program)(uint32_t Addr, uint8_t *Data, uint16_t Len);
		uint8_t (*Read)(uint32_t Addr, uint8_t *Data, uint16_t Len);
	} par_log_type;

	void par_log_save(const par_log_type *cfg, uint16_t pos, void *val);
	void par_log_load(const par_log_type *cfg);
	uint8_t par_log_tab_save(const par_log_type *cfg);

#ifdef __cplusplus
}
#endif

#endif
